package cn.com.bluemoon.daps.model.core.file.export.impl;

import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.toolkit.BmAssetUtils;
import cn.com.bluemoon.daps.domp.api.FastDFSService;
import cn.com.bluemoon.daps.model.core.file.export.BaseFileExport;
import cn.com.bluemoon.daps.model.core.file.export.FileExport;
import lombok.extern.slf4j.Slf4j;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * 文件上传到fastdfs 返回url，提供给前端下载
 * 需要依赖微服务的{@link FastDFSService}
 *
 * @author Jarod.Kong
 * @date 2021/3/23 9:48
 */
@Slf4j
public class FastDfsFileExport extends BaseFileExport<FastDFSService.UploadResult> {
    private final FastDFSService fastDFSService;

    public FastDfsFileExport(FastDFSService fastDFSService) {
        BmAssetUtils.notNull(fastDFSService, "文件上传至FastDfs必须指定fastDFSService");
        this.fastDFSService = fastDFSService;
    }

    /**
     * 导出动作
     *
     * @param fileExport 导出对象
     * @return 结果
     */
    @Override
    public ResultBean<FastDFSService.UploadResult> export(FileExport fileExport) {
        check0(fileExport);
        FastDFSService.FastDfsMultipartFile multipartFile = null;
        try {
            multipartFile = new FastDFSService.FastDfsMultipartFile("file", fileExport.getFileName(), "multipart/form-data", new FileInputStream(fileExport.getFile()));
        } catch (IOException e) {
            log.error("构建FastDfsMultipartFile实例失败！！", e);
        }
        FastDFSService.UploadResult uploadResult = fastDFSService.uploadFile0(multipartFile);
        return ResultBean.ok(uploadResult);
    }
}
